package net.ceviz.forum.java;

import java.io.*;
import java.net.*;
import java.nio.CharBuffer;

public class HttpMirror {

	public static void main(String[] args) {

		try {
			// Get the port.
			int port = Integer.parseInt(args[0]);

			// Create a socket for listening that port.
			ServerSocket ss = new ServerSocket(port);

			for (;;) {

				/*
				 * Wait for connection from the client. Routine will be blocked
				 * when it returns the socket that connected to the client.
				 */
				Socket client = ss.accept();

				// Set the input and output in order to talk with the client.
				BufferedReader in = new BufferedReader(new InputStreamReader(
						client.getInputStream()));
				PrintWriter out = new PrintWriter(client.getOutputStream());
				
				String crlf = "\r\n";
				out.print("HTTP/1.1 200 OK" + crlf);
				out.print("Content-Type text/plain" + crlf);

				/*
				 * At that point, read HTTP request and send it back to the
				 * client.
				 */
				out.print(crlf);
				String line;
				while ((line = in.readLine()) != null) {
					if (line.length() == 0)
						break;
					out.print(line + crlf);
					System.out.println(line);
				}
				
				
//				char[] buffer = new char[1000];
//				while(in.read(buffer) != -1) {
//					System.out.print(buffer);
//				}
				
//				out.flush();
				System.out.println("CLOSING CONNECTIONS");
				out.close();
				in.close();
				client.close();

			} // Tekrardöngüyegirvebirsonrakibağlantıyıbekle.
		} catch (Exception e) {
			System.err.println(e);
		}

	}

}